﻿namespace PlugNT.Common
{
    using System;
    using System.Collections;
    using System.Web;
    using System.Web.Caching;

    public class DataCache
    {
        private static int Factor = 1;

        public static void Clear()
        {
            Cache objCache = HttpRuntime.Cache;
            IDictionaryEnumerator enumerator = objCache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                objCache.Remove(enumerator.Key.ToString());
            }
        }

        public static object GetCache(string CacheKey)
        {
            return HttpRuntime.Cache[CacheKey];
        }

        public static string Help()
        {
            return "mailto:354990393@qq.com";
        }

        public static void Remove(string key)
        {
            HttpRuntime.Cache.Remove(key);
        }

        public static void RemoveLike(string like_key)
        {
            Cache objCache = HttpRuntime.Cache;
            IDictionaryEnumerator enumerator = objCache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (enumerator.Key.ToString().StartsWith(like_key))
                {
                    objCache.Remove(enumerator.Key.ToString());
                }
            }
        }

        public static void SetCache(string key, object obj)
        {
            Cache objCache = HttpRuntime.Cache;
            if ((objCache[key] == null) && (obj != null))
            {
                objCache.Insert(key, obj);
            }
        }

        public static void SetCache(string key, object obj, int seconds)
        {
            Cache objCache = HttpRuntime.Cache;
            if ((objCache[key] == null) && (obj != null))
            {
                objCache.Insert(key, obj, null, DateTime.Now.AddSeconds((double) (Factor * seconds)), TimeSpan.Zero, CacheItemPriority.Normal, null);
            }
        }
    }
}

